//////////////////////////////////////////////
//基础路径
const LOCAL_IP_URL = 'http://192.168.10.16:8080' // ipconfig 获取本机ip 测试用
const LOCAL_BASE_URL = 'http://localhost:8080'
const BASE_URL = 'https://frp-act.com:36574' // https://frp-act.com:36574
//完全路径（由方法中自动拼接BASE_URL和外部传参）
let URL = ''
//////////////////////////////////////////////
//请求方式
let METHOD = 'GET'
const GET_METHOD = 'GET'
const POST_METHOD = 'POST'
const PUT_METHOD = 'PUT'
const DELETE_METHOD = 'DELETE'
//////////////////////////////////////////////
//请求头
let HEADER = {}
//请求体
let DATA = {}
//////////////////////////////////////////////

// 请求拦截器数组
const requestInterceptors = []
// 响应拦截器数组
const responseInterceptors = []

//获取配置 添加全局配置
const getConfig = (config = {}) => {
	let {
		url,
		//请求方法如果没传递 默认为GET
		method = GET_METHOD,
		header = {},
		data = {}
	} = config

	//请求地址
	URL = LOCAL_BASE_URL + url;
	//请求方式
	METHOD = method
	
	//请求头添加参数
	header['access-key'] = 'liming0319';
	//请求头
	HEADER = header;

	//请求体
	DATA = data;

	// 执行请求拦截器
	const requestConfig = { url: URL, method: METHOD, header: HEADER, data: DATA }
	requestInterceptors.forEach(interceptor => {
		const result = interceptor(requestConfig)
		if (result) {
			URL = result.url
			METHOD = result.method
			HEADER = result.header
			DATA = result.data
		}
	})
}

//发送请求
const getPromiseBySendRequest = () => {
	return new Promise(async (resolve, reject) => {
		try {
			let res = await uni.request({
				url: URL,
				method: METHOD,
				header: HEADER,
				data: DATA
			})

			// 执行响应拦截器
			let interceptedRes = res
			responseInterceptors.forEach(interceptor => {
				const result = interceptor(interceptedRes)
				if (result) {
					interceptedRes = result
				}
			})

			if (interceptedRes.data.code === 1) {
				resolve(interceptedRes.data.data)
			} else if (interceptedRes.data.code === -1) {
				uni.showModal({
					title: '错误提示',
					content: interceptedRes.data.message,
					showCancel: false
				})
				reject(interceptedRes.data)
			} else {
				uni.showToast({
					title: interceptedRes.data.message,
					icon: 'none'
				})
				reject(interceptedRes.data)
			}
		} catch (e) {
			//TODO handle the exception
			console.log("请求结果出错："+e.message)
			reject(e)
		}
	});
}

// 定义Request类
class Request {
	// 构造函数（虽然在这个例子中不需要初始化实例属性）
	constructor() {}

	// 添加请求拦截器
	static addRequestInterceptor(interceptor) {
		requestInterceptors.push(interceptor)
	}

	// 添加响应拦截器
	static addResponseInterceptor(interceptor) {
		responseInterceptors.push(interceptor)
	}

	static request = (config = {}) => {
		//获取配置
		getConfig(config);
		return getPromiseBySendRequest();
	}

	//get请求
	static get = (config = {}) => {
		//获取配置
		getConfig(config)
		METHOD = GET_METHOD
		//发送请求
		return getPromiseBySendRequest();
	}

	//post请求
	static post = (config = {}) => {
		//获取配置
		getConfig(config)
		METHOD = POST_METHOD
		//发送请求
		return getPromiseBySendRequest();
	}

	//put请求
	static put = (config = {}) => {
		//获取配置
		getConfig(config)
		METHOD = PUT_METHOD
		//发送请求
		return getPromiseBySendRequest();
	}

	//delete请求
	static delete = (config = {}) => {
		//获取配置
		getConfig(config)
		METHOD = DELETE_METHOD
		//发送请求
		return getPromiseBySendRequest();
	}

}


export default Request;